/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.chat;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.RawInputListener;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import com.jme3.scene.control.AbstractControl;

/**
 * 
 * @author mattias
 */
public class ChatState extends AbstractAppState implements ActionListener {

	private SimpleApplication app;
	private InputManager inputManager;
	private ChatDisplay chat;
	private AbstractControl lastState;
	private RawListener inputlistener;

	public ChatState(ChatDisplay chat, AbstractControl lastState) {
		this.chat = chat;
		this.lastState = lastState;
		inputlistener = new RawListener();

	}

	@Override
	public void initialize(AppStateManager stateManager, Application app) {
		super.initialize(stateManager, app);

		this.app = (SimpleApplication) app;
		this.inputManager = this.app.getInputManager();
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);

		if (enabled) {
			inputManager.addRawInputListener(inputlistener);
			inputManager.addMapping("end_chat",
					new KeyTrigger(KeyInput.KEY_TAB));
			inputManager.addListener(this, "end_chat");
			inputManager.addMapping("send_chat", new KeyTrigger(
					KeyInput.KEY_RETURN));
			inputManager.addListener(this, "send_chat");
			inputManager.addMapping("back", new KeyTrigger(KeyInput.KEY_BACK));
			inputManager.addListener(this, "back");
		} else {
			inputManager.removeRawInputListener(inputlistener);
			inputManager.deleteMapping("end_chat");
			inputManager.deleteMapping("send_chat");
			inputManager.deleteMapping("back");
			inputManager.removeListener(this);
		}
	}

	public void onAction(String name, boolean isPressed, float tpf) {
		if (name.equals("send_chat") && isPressed) {
			chat.appendMessage();
			setEnabled(false);
			lastState.setEnabled(true);
		} else if (name.equals("end_chat") && isPressed) {
			chat.clearWriting();
			setEnabled(false);
			lastState.setEnabled(true);
		} else if (name.equals("back") && isPressed) {
			chat.back();
		}
	}

	private class RawListener implements RawInputListener {

		public void beginInput() {
			chat.write();
		}

		public void endInput() {
		}

		public void onJoyAxisEvent(JoyAxisEvent evt) {
		}

		public void onJoyButtonEvent(JoyButtonEvent evt) {
		}

		public void onMouseMotionEvent(MouseMotionEvent evt) {
		}

		public void onMouseButtonEvent(MouseButtonEvent evt) {
		}

		public void onKeyEvent(KeyInputEvent evt) {
			if (evt.isPressed()) {
				chat.write(evt.getKeyChar());
			}
		}

		public void onTouchEvent(TouchEvent evt) {
		}
	}
}
